package com.jason.quectel0805.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import com.jason.quectel0805.R;
import com.jason.quectel0805.entities.Select;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @author：Jason
 * @date：2022/8/10 15:57
 * @email：1129847330@qq.com
 * @description:
 */
public class SelectAdapter extends RecyclerView.Adapter<SelectAdapter.SelectViewHolder> {
    private List<Select> list;
    private Context mContext;
    private MyAdapter.OnItemClickListener onItemClickListener;
    private MyAdapter.OnItemLongClickListener onItemLongClickListener;
    private boolean isShowCheckBox = false;

    public boolean isShowCheckBox() {
        return isShowCheckBox;
    }

    public void setShowCheckBox(boolean showCheckBox) {
        isShowCheckBox = showCheckBox;
    }

    public void setOnItemLongClickListener(MyAdapter.OnItemLongClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }

    public void setOnItemClickListener(MyAdapter.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public List<Select> getList() {
        return list;
    }

    private static final String TAG = "jasonwan";

    public SelectAdapter(List<Select> list, Context context) {
        this.list = list;
        mContext = context;
    }

    @NonNull
    @Override
    public SelectAdapter.SelectViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_select, null, false);
        SelectAdapter.SelectViewHolder myViewHolder = new SelectAdapter.SelectViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull SelectAdapter.SelectViewHolder holder, int position) {
        Select select = list.get(position);
        holder.tvShow.setText(select.content);
        int adapterPosition = holder.getAdapterPosition();
        holder.cbSelect.setChecked(select.isSelected);
        if (isShowCheckBox) {
            holder.cbSelect.setVisibility(View.VISIBLE);
        } else {
            holder.cbSelect.setVisibility(View.INVISIBLE);
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //    单击之后的需求是不明确的
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(holder.itemView, adapterPosition);
                }
            }
        });
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (onItemLongClickListener != null) {
                    return onItemLongClickListener.onItemLongClick(holder.itemView, adapterPosition);
                }
                return true;
            }
        });
    }

    //item
    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    static class SelectViewHolder extends RecyclerView.ViewHolder {
        private TextView tvShow;
        private CheckBox cbSelect;
        private View itemView;

        public SelectViewHolder(@NonNull View itemView) {
            super(itemView);
            this.itemView = itemView;
            tvShow = itemView.findViewById(R.id.tv_select_show);
            cbSelect = itemView.findViewById(R.id.cb_select);
        }
    }
}